
package com.yimixian.app.adapter;

import com.yimixian.app.EventBusDomain.PlusMinusIconMessage;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.Constants;
import com.yimixian.app.goods.BannersView;
import com.yimixian.app.goods.GoodsView;
import com.yimixian.app.goods.GoodsViewModel;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.GlobalLayoutUtils;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class GoodsItemListViewAdapter extends BaseAdapter {

    private List<GoodsViewModel> mGoodsViewModels;
    private Activity mActivity;
    private CartManager mCartManager;
    private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;
    private Fragment mFragment;

    public GoodsItemListViewAdapter(List<GoodsViewModel> viewModels, Activity activity,
            GoodsItemPresenter.OnPlusButtonClickListener plusButtonClickListener,
            CartManager cartManager, Fragment fragment) {
        mGoodsViewModels = new ArrayList<>(viewModels);
        mActivity = activity;
        mOnPlusButtonClickListener = plusButtonClickListener;
        mCartManager = cartManager;
        mFragment = fragment;
//        EventBus.getDefault().register(this);
    }

    @Override
    public int getCount() {
        return mGoodsViewModels.size();
    }

    @Override
    public GoodsViewModel getItem(int position) {
        return mGoodsViewModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //这个是有Banner时的view
//        return getItemViewType(position) == 0 ?
//                getBannersView(position, convertView) :
//                getGoodsView(position, convertView);
        return getGoodsView(position, convertView);
    }

    private View getBannersView(final int position, View convertView) {
        if (convertView == null) {
            convertView = new BannersView(mActivity);
        }
        final BannersView bannersView = (BannersView) convertView;
        GlobalLayoutUtils.runAfterLayout(bannersView, new Runnable() {
            @Override
            public void run() {
                bannersView.bind(getItem(position).banners);
            }
        });
        bannersView.setImageRatio(getItem(position).bannersRatio);
        return bannersView;
    }

    private View getGoodsView(int position, View convertView) {
        if (convertView == null) {
            convertView = new GoodsView(mActivity);
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        GoodsView goodsView = (GoodsView) convertView;
        goodsView.setFragment(mFragment);
        goodsView.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
        goodsView.bind(getItem(position).goods);
        return goodsView;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

//    @Override
//    public int getViewTypeCount() {
//        return 2;
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        return getItem(position).banners != null ? 0 : 1;
//    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public void removeAllList() {
        mGoodsViewModels.clear();
    }

    public void setGoodsViewModels(List<GoodsViewModel> goodsViewModels) {
        mGoodsViewModels = goodsViewModels;
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
    }
}
